package com.woniuxy01;

import java.io.*;
import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-06-10 10:39
 */
public class Test03 {
    public static void main(String[] args) throws IOException {


    }
    
    private static void test08() {
       //try-with-resources
        try( FileInputStream fis =new FileInputStream("a.txt");) {
            byte[] bytes=new byte[2];
            int len;
            while((len=fis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void test07() {
        FileInputStream fis =null;
        try {
            fis = new FileInputStream("a.txt");
            byte[] bytes=new byte[2];
            int len;
            while((len=fis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void test06() throws IOException {
        FileInputStream fis = new FileInputStream("a.txt");
        //一次读取一个字节数组,一般设置为1024的整数倍
        //public int read(byte b[]):
        //参数数组，存储读取到的数据
        //返回值代表读取了几个有效数据，-1代表没有数据了
        byte[] bytes=new byte[2];
        int len;
        while((len=fis.read(bytes))!=-1){
//            System.out.println(Arrays.toString(bytes));
            System.out.println(new String(bytes,0,len));
        }
        fis.close();
    }

    private static void test05() throws IOException {
        //字节流读取文件
        FileInputStream fis = new FileInputStream("a.txt");
        //一次读取一个字节
        int mybyte;
        //read()返回值-1，读取结束；不是-1，就是当前读到的字节编码
        while ((mybyte = fis.read()) != -1) {
            System.out.println((char)mybyte);
        }
        fis.close();
    }

    private static void test04() {
        FileOutputStream fos =null;
        try {
            fos = new FileOutputStream("a.txt");
            fos.write("abc中文".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void test03() throws IOException {
        //使用字节流，写入数据
        FileOutputStream fos = new FileOutputStream("a.txt");
        //一次写一个字符串
        fos.write("abc".getBytes());
        fos.write("中文".getBytes());
        fos.close();
    }

    private static void test02() throws IOException {
        //使用字节流，写入数据
        FileOutputStream fos = new FileOutputStream("a.txt");
        //一次写一个字节数组
        byte[] bytes={97,98,'c','d','e'};
        fos.write(bytes);
        fos.close();
    }

    private static void test01() throws IOException {
        //使用字节流，写入数据
//        File file=new File("d:\\aaa\\a.txt");
//        FileOutputStream fos = new FileOutputStream(file);
        //IO绝对路径:从磁盘根目录开始写
        //IO相对路径:相对当前项目的根目录
        FileOutputStream fos = new FileOutputStream("a.txt");
        //一次写一个字节
        fos.write(97);
        fos.write('b');
        fos.write('c');
        //IO操作结束以后关闭资源
        fos.close();
    }
}
